// @ 516
// _(:з」∠)_

define([ ], function() {
    'use strict';

    const Legend = {};

    Legend["elmt"] = function( param ) {
        let spcItemHtml = ``,
            legendItemHtml = ``,
            main = document.querySelector(".main");

        for ( let x = 0; x < param.length; x++ ) {
            spcItemHtml += `<li class="legend_item" value="${ param[x].start }" style="background-color: ${ param[x].color };">
                                <span>${ param[x].value }</span>
                            </li>`;
        }

        let legendHtml = `<div class="legend legend_elmt">
                            <div class="legend_head">图例</div>
                            <div class="legend_main">
                                <ul class="legend_list">
                                    ${ legendItemHtml }
                                    ${ spcItemHtml }
                                </ul>
                            </div>
                        </div>`;

        let legendBox = main.querySelector(".legend");
        if ( legendBox ) legendBox.remove();
        main.insertAdjacentHTML( "beforeEnd", legendHtml );
    }

    Legend["level"] = function( param ) {
        let spcItemHtml = ``,
            legendItemHtml = ``,
            main = document.querySelector(".main");

        for ( let x = 0; x < param.length; x++ ) {
            if ( param[x].Caption != "无影响" ) {
                legendItemHtml += `<li class="legend_item" value="${ param[x].Value }" style="background-color: ${ param[x].FillStyle.ForeColor };">
                                        <span>${ param[x].Caption }</span>
                                    </li>`;
            } else {
                spcItemHtml += `<li class="legend_item" value="${ param[x].Value }" style="background-color: ${ param[x].FillStyle.ForeColor };">
                                    <span>${ param[x].Caption }</span>
                                </li>`;
            }
        }

        let legendHtml = `<div class="legend legend_level">
                            <div class="legend_head">图例</div>
                            <div class="legend_main">
                                <ul class="legend_list">
                                    ${ legendItemHtml }
                                    ${ spcItemHtml }
                                </ul>
                            </div>
                        </div>`;

        let legendBox = main.querySelector(".legend");
        if ( legendBox ) legendBox.remove();
        main.insertAdjacentHTML( "beforeEnd", legendHtml );

        // 触发点击事件
        let legendItem = document.querySelectorAll(".legend_item");
        for ( let x = 0; x < legendItem.length; x++ ) {
            legendItem[x].addEventListener( "click", function(e) {
                if ( legendItem[x].hasClass("select") ) {
                    filterFeature( legendItem[x].getAttribute("value"), false );
                    legendItem[x].removeClass("select");
                } else {
                    filterFeature( legendItem[x].getAttribute("value"), true );
                    legendItem[x].addClass("select");
                }
            }, false);
        }
    }

    function filterFeature( value, status ) {
        // 过滤图层属性获取feature
        let conArry = [],
        layerArry = MC.mapAllLayer;

        for ( let y in layerArry ) {
            if ( layerArry[y].feature ) conArry.push( layerArry[y] );
        }

        for ( let y = 0; y < conArry.length; y++ ) {
            let minValue = conArry[y].feature.properties.MGMinValue;
            if ( minValue ) {
                if ( status ) {
                    if ( minValue <= Number(value) && MC.map.hasLayer( conArry[y] ) ) {
                        MC.map.removeLayer( conArry[y] );
                    }
                } else {
                    if ( minValue <= Number(value) && !MC.map.hasLayer( conArry[y] ) ) {
                        MC.map.addLayer( conArry[y] );
                    }
                }
            }
        }
    }

    window.Legend = Legend;

    return Legend;
});